package com.ruoyi.web.controller.okr;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.resultMapper.OAlignResult;
import com.ruoyi.system.domain.resultMapper.OAlignedResult;
import com.ruoyi.system.service.IOkrAlignInfoService;
import com.ruoyi.system.service.IOkrInfoService;
import com.ruoyi.system.service.IOkrInformService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Bruce on 2021/8/5 21:43
 **/
@Controller
@RequestMapping("/okrAlign")
public class MyOkrAlignController extends BaseController {
    private String prefix = "okr/";

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IOkrInfoService okrInfoService;

    @Autowired
    private IOkrAlignInfoService okrAlignInfoService;

    @Autowired
    private IOkrInformService okrInformService;

    private OkrAlignInfo okrAlignInfo;


    private AlignBase alignBase01 = new AlignBase();
    private AlignBase alignBase02 = new AlignBase();


    @GetMapping()
    public String show(){
        return prefix + "/dataKanban";
    }


    @GetMapping("/allObjective/{id}")
    public String showObjective(@PathVariable("id") Long id, Model model){
        SysUser user = ShiroUtils.getSysUser();
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(id);
        alignBase01.setOkrId(id);
        alignBase01.setUserId(user.getUserId());
        alignBase01.setDeptId(user.getDeptId());
        alignBase01.setCycleId(okrInfo.getCycleId());
        model.addAttribute("source", id);
        return prefix + "addAlign";
    }

    @PostMapping("/ObjList")
    @ResponseBody
    public TableDataInfo list(){
        startPage();
        List<OAlignResult> list = okrAlignInfoService.queryAllAlignO(alignBase01);
        return getDataTable(list);
    }

    @Log(title = "添加OKR对齐关系", businessType = BusinessType.GRANT)
    @PostMapping("/addAlign")
    @ResponseBody
    public AjaxResult addAlign(OkrAlignInfo okrAlignInfo, Long userId){
        if (okrAlignInfoService.queryReverseAlign(okrAlignInfo.getOkrId(), okrAlignInfo.getParentId()) > 0){
            return AjaxResult.error("对方该Objective已经对齐你了，不允许反向对齐");
        }
        okrAlignInfoService.insertOkrAlignInfo(okrAlignInfo);
        //被对齐的OKR
        OkrInfo okrInfo = okrInfoService.selectOkrInfoById(okrAlignInfo.getParentId());
        //准备对齐的OKR
        OkrInfo alignOkr = okrInfoService.selectOkrInfoById(okrAlignInfo.getOkrId());
        SysUser user = ShiroUtils.getSysUser();
        OkrInform okrInform = new OkrInform();
        okrInform.setParentId(user.getUserId());
        okrInform.setInformTime(DateUtils.getNowDate());
        okrInform.setParentName(user.getUserName());
        okrInform.setUserId(okrInfo.getUserId());
        okrInform.setUrl("okr/info/view/" + alignOkr.getId());
        okrInform.setContent("将目标对齐了您的目标[" + okrInfo.getContent() + "]");
        okrInform.setAvatar(user.getAvatar());
        okrInform.setType("对齐");
        okrInformService.insertOkrInform(okrInform);
        return AjaxResult.success("添加对齐成功");
    }

    @GetMapping("/myAlign/{id}")
    public String showMyAlign(@PathVariable("id") Long id, Model model){
        alignBase02.setOkrId(id);
        model.addAttribute("source", id);
        return prefix + "cancelAlign";
    }

    @PostMapping("/alignedOkr")
    @ResponseBody
    public TableDataInfo queryAlignedOkr(OkrAlignInfo okrAlignInfo){
        List<OAlignedResult> oAlignedResults = okrAlignInfoService.queryAlignedO(alignBase02);
        System.out.println("oAlignedResults   " + oAlignedResults);
        return getDataTable(oAlignedResults);
    }

    @PostMapping("/cancelAlign")
    @ResponseBody
    public AjaxResult cancelAlign(OkrAlignInfo okrAlignInfo){
        Long id = okrAlignInfoService.queryByAlignId(okrAlignInfo);
        okrAlignInfoService.deleteOkrAlignInfoById(id);
        return AjaxResult.success("对齐取消成功");
    }

}
